package com.sy.controller;

import com.sy.dto.PageBean;
import com.sy.entity.HomeFilePath;
import com.sy.entity.Homework;
import com.sy.service.HomeWorkService;
import com.sy.service.StudentService;
import com.sy.vo.HomeworkUpdateVo;
import com.sy.vo.HomeworkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:fma
 * @Date: 2021/10/12 11:22
 */
@RestController
public class HomeWorkController {
    @Autowired
    private HomeWorkService homeWorkService;

    /**
     * @Author:fma
     * 获取全部标题名
     * 路径名 http://localhost:8888/homeworks
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET, params = {"startLimit", "endLimit"})
    public List<Homework> getAllHomes(@RequestParam(name = "startLimit", required = false) Integer startLimit,
                                      @RequestParam(name = "endLimit", required = false) Integer endLimit) {
        return homeWorkService.getAllHomes(startLimit, endLimit);
    }

    /**
     * @Author:fma
     * 获取全部标题名
     * 路径名 http://localhost:8888/homeworks?sno=10001&pageNo=1&pageNum=5
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET, params = {"sno", "pageNo", "pageNum"})
    public PageBean<Homework> getAllHomesBySno(@RequestParam(name = "sno") Integer sno,
                                               @RequestParam(name = "pageNo", required = false) Integer pageNo,
                                               @RequestParam(name = "pageNum", required = false) Integer pageNum) {
        return homeWorkService.getAllHomesBySno(sno, pageNo, pageNum);
    }

    /**
     * 根据教师的完整姓名和模糊作业关键字进行查询
     * @Author:fma
     * @param theName  教师姓名
     * @param theTitle 作业标题
     * @param pageNum  显示条数
     * @param pageNo   页码
     * @return 根据条件查询出的商品信息的分页结果
     * 请求路劲 http://localhost:8888/getHomeworks?sno=10001&title=&createDate=
     */
    @RequestMapping(value = "homework", method = RequestMethod.GET, params = {"sno", "theName", "theTitle", "pageNo", "pageNum"})
    public PageBean<Homework> getHomeWorksByConditions(@RequestParam(name = "sno") Integer sno,
                                                       @RequestParam(name = "theName", required = false) String theName,
                                                       @RequestParam(name = "theTitle", required = false) String theTitle,
                                                       @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageNum", required = false, defaultValue = "5") Integer pageNum) {
        return homeWorkService.getHomeWorksByConditions(sno, theName, theTitle, pageNo, pageNum);
    }

    /**
     * @Author:fma
     * 获取当前教师布置的作业
     * 前端请求路劲  http://localhost:8888/homeworks?tno=2
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET, params = {"tno"})
    public List<Homework> getHomeByTno(@RequestParam("tno") Integer tno) {
        return homeWorkService.getHomeByTno(tno);
    }

    /**
     * @Author:fma
     * 获取当前id指定的作业
     * 前端请求路劲  http://localhost:8888/homeworks?id=
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET, params = {"id"})
    public Homework getHomeById(@RequestParam("id") Integer id) {
        return homeWorkService.getHomeById(id);
    }

    /**
     * @Author:fma
     * 回显初始化全部作业的数量 TODO...根据指定教师回显作业的数量待定
     * 前端请求路劲  http://localhost:8888/homeworks
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET)
    public Integer getAllHomes() {
        return homeWorkService.getAllHomes();
    }



    /**
     * @Author 范富强
     * 判断发布作业题目是否存在
     *
     * @param title
     * @return
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET, params = {"title"})
    public void isTitleExists(@RequestParam("title") String title) {
        homeWorkService.selectHomeworkByTitle(title);
    }

    /**
     * @Author 范富强
     * 发布作业
     * @param homeworkvo
     * @return 发布作业是否成功
     */
    @RequestMapping(value = "homeworkPublish", method = RequestMethod.POST)
    public boolean publishHomework(@RequestBody HomeworkVo homeworkvo) {
        return homeWorkService.publishHomework(homeworkvo);
    }


    /**
     * @Author 范富强
     * 删除发布的作业
     * <p>
     * 删除发布的作业
     *
     * @param id 发布的作业的Id
     * @return 删除作业是否成功
     */
    @RequestMapping(value = "homerworks", method = RequestMethod.DELETE)
    public boolean deleteHomeworkById(@RequestParam Integer id) {
        return homeWorkService.deleteHomeworkById(id);
    }

    /**
     * @param tno    分类id
     * @param pageNo 页码
     *               前端请求路劲  http://localhost:8888/homeworks/teacherIds?tno=
     * @Author 范富强
     * 获取当前id指定的作业并分页显示
     * * 根据教师的完整姓名和模糊作业关键字或者发布时间进行查询
     */
    @RequestMapping(value = "homeworks/teacherIds", method = RequestMethod.GET, params = {"tno", "title", "pageNo", "pageNum"})
    public PageBean<Homework> getHomeworkByConditions(@RequestParam(value = "tno") Integer tno,
                                                      @RequestParam(name = "title", required = false) String title,
                                                      @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageNum", required = false, defaultValue = "5") Integer pageNum) {
        return homeWorkService.getHomeWorksByConditions2(tno, title, pageNo, pageNum);

    }

    /**
     * @Author:cxl
     * @Date: 2021/10/12 11:22
     */
    @RequestMapping(value = "homeworks", method = RequestMethod.GET, params = {"hid"})
    public List<String> getHomeFilePathsByHid(@RequestParam("hid") Integer hid) {
        return homeWorkService.getHomeFilePathsByHid(hid);
    }

    /**
     * @param homeworkUpdateVo
     * @return 是否将数据更新成功
     * @Author 范富强
     */
    @RequestMapping(value = "updateHomework", method = RequestMethod.POST)
    public boolean updateHomeWork(@RequestBody HomeworkUpdateVo homeworkUpdateVo) {
        return homeWorkService.updateHomeWork(homeworkUpdateVo);
    }

    /**
     * @Author 范富强
     * @param id 作业的id
     * @param mark 作业的评分
     * @return  更新数据库的表是否成功
     */
    @RequestMapping(value = "homeworkScore", method = RequestMethod.POST, params = {"id", "mark"})
    public boolean homeworkScore(@RequestParam("id") Integer id, @RequestParam("mark") Integer mark) {
        return homeWorkService.updateHomeWorkMarks(id,mark);
    }

    /**
     * @Author 范富强
     * @param hid 作业的id
     * @param sno 学生的学号
     * @return 学生提交的作业的路径
     */
    @RequestMapping(value = "homeworks" ,method = RequestMethod.GET,params = {"hid","sno"})
    public List<String> getHomeFilePathsByHid2(@RequestParam("hid") Integer hid,
                                              @RequestParam("sno") Integer sno){
        System.out.println(hid);
        System.out.println(sno);
        return homeWorkService.getHomeFilePathsByHid2(hid,sno);
    }

    /**
     * @Author 范富强
     *
     * @param hid 作业的id
     * @return 这个作业的标题
     */
    @RequestMapping(value = "homeworks/title" ,method = RequestMethod.GET,params = "hid")
    public String getTitleByHid(@RequestParam("hid") Integer hid){
        System.out.println(hid);
        return "";
    }
}
